﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.ISV.MO;
    using UFIDA.U9.ISV.MO.Proxy;
    using UFIDA.U9.MO.Complete;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Transactions;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// CompRptSaveSV partial 
	/// </summary>	
	public partial class CompRptSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new CompRptSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class CompRptSaveSVImpementStrategy : BaseStrategy
	{
		public CompRptSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			CompRptSaveSV bpObj = (CompRptSaveSV)obj;

			//启用UBF事务
			//using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
			//{
				try
				{
					if (bpObj.Data == null || bpObj.Data.Count == 0)
						throw new Exception("传入参数为空");
					List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

					PreSave(bpObj.Data);
					List<object> inputdtos = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item => item as object)
						, typeof(CompRptDTOData));
					foreach (CompRptDTOData dto in inputdtos)
					{
						CompleteRpt item = CompleteRpt.Finder.Find($"DocNo='{dto.DocNo}'");
						if (item == null)
						{
							//新增逻辑
							CreateCompRptSrvProxy proxy = new CreateCompRptSrvProxy();
							proxy.CompRptDTOs = new List<CompRptDTOData> { dto };
							List<CompRptKeyDTOData> rets = proxy.Do();
							if (rets.Count == 0)
								throw new Exception("生成完工报告失败!");
							if (!rets[0].OperateResult)
								throw new Exception(rets[0].ErrorMsg);

							//修改单号并返回
							rets[0].DocNo = EntityUtils.UpdateDocNo<CompleteRpt>(rets[0].ID, dto.DocNo);
							retlist.Add(new CommonArchiveDataDTOData
							{
								ID = rets[0].ID,
								Code = rets[0].DocNo,
							});

							//自动审核
							CompRptApproveSV approveSV = new CompRptApproveSV();
							approveSV.Data = new List<CopyOfCompRptKeyDTOData>
							{
								new CopyOfCompRptKeyDTOData
								{
									DocNo = rets[0].DocNo,
									OperateType=true
								}
							};
							ResultDTOData resultDTOData = approveSV.Do();
							ResultUtils.CheckResult(resultDTOData);
						}
						else
						{
							//修改逻辑
							throw new AITException(599, $"单号:{dto.DocNo}已经存在,不能生成!");
						}
					}
					//scope.Commit();
					return ResultUtils.success(bpObj, bpObj.Data, retlist);
				}
				catch (Exception ex)
				{
					//scope.Rollback();
					return ResultUtils.error(bpObj, bpObj.Data, ex);
				}
			//}
			
		}

        private void PreSave(List<CopyOfCompRptDTOData> data)
        {
            foreach(var dto in data)
            {
				//校验
				if (dto.DocNo == null || dto.DocNo.Length == 0)
					throw new Exception($"传入单号[DocNo]为空");
				if (dto.ItemMaster == null)
					throw new Exception($"传入料品[ItemMaster]为空");
				if (dto.MO == null)
					throw new Exception($"传入生产订单[MO]为空");
                UFIDA.U9.MO.MO.MO mo = UFIDA.U9.MO.MO.MO.Finder.Find($"DocNo='{dto.MO.Code}'");
				if (mo == null)
					throw new Exception($"查找生产订单失败!订单号:[{dto.MO.Code}]");

				if (dto.ItemMaster.Code != mo.ItemMaster.Code)
					throw new Exception($"生产订单[{mo.DocNo}]料品[{mo.ItemMaster.Code}]与传入料品[{dto.ItemMaster.Code}]不同");
            }
        }
    }

	#endregion
	
	
}